using System;

namespace Byleist.Caching
{
    public class RequestAmountInvalidationPolicy : ICacheInvalidationPolicy
    {
        private readonly int maxAmountOfCacheRequests;
        private int currentAmountOfCacheRequests;

        public RequestAmountInvalidationPolicy(int maxAmountOfCacheRequests)
        {
            if (maxAmountOfCacheRequests <= 0)
            {
                throw new ArgumentOutOfRangeException("maxAmountOfCacheRequests", "maxAmountOfCacheRequests must be greater than 0");
            }
            this.maxAmountOfCacheRequests = maxAmountOfCacheRequests;
        }

        public void CacheRequested()
        {
            this.currentAmountOfCacheRequests++;
        }

        public void InvalidateCache()
        {
            this.currentAmountOfCacheRequests = this.maxAmountOfCacheRequests + 1;
        }

        public bool IsCacheValid()
        {
            return this.currentAmountOfCacheRequests <= this.maxAmountOfCacheRequests;
        }

        public void CacheRenewed()
        {
            this.currentAmountOfCacheRequests = 0;
        }
    }
}